package ru.ifmo.ctd.intsys.afanasyeva.neural.digits;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class ImageUtils {
	public static double[] imageToBits(String filename, int width, int height) throws IOException {
		BufferedImage img = ImageIO.read(new File(filename));
		img.getScaledInstance(width, height, 0);

		double[] bits = new double[width * height];
		int k = 0;
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				bits[k] = img.getRGB(j, i);// == -1 ? 0 : 1;
				k++;
			}
		}
	
		return bits;
	}
	
	public static int toAnswer(double value) {
		return (int)(value * 10 + 0.5);
	}
	
	public static void main(String[] args) throws IOException {
		String filename = "data/neural/0.bmp";
		BufferedImage img = ImageIO.read(new File(filename));
		
		int width = img.getWidth();
		int height = img.getHeight();
		double[] bits = imageToBits(filename, 3, 5);
		
		int k = 0;
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				System.out.print(bits[k]);
				k++;
			}
			System.out.println();
		}
	}
}
